<?php
/**
 * 春哥团队（www.cgtblog.com） - 春哥多端多商户万能DIY商城系统
 * Copyright © 2024 春哥技术团队 保留所有权利
 * =========================================================

 * ----------------------------------------------
 * 您只能在商业授权范围内使用，不可二次转售、分发、分享、传播
 * 任何企业和个人不得对代码以任何目的任何形式的再发布
 * =========================================================
 */

//管理员中心 - 会员列表
namespace app\controller;
use think\facade\Db;
class ApiAdminMember extends ApiAdmin
{	
	public function initialize(){
		parent::initialize();
        if(!in_array(request()->action(),['searchCode','detail','decscore'])){
            if(bid != 0) die(json_encode(['status'=>-4,'msg'=>'无权限操作']));
        }
	}
	public function index(){
		$pagenum = input('post.pagenum');
		if(!$pagenum) $pagenum = 1;
		$pernum = 20;
		$where = [];
		$where[] = ['aid','=',aid];
		if(input('param.keyword')){
			$where[] = ['id|nickname|realname|tel','like','%'.input('param.keyword').'%'];
		}
		$datalist = Db::name('member')->field('id,aid,bid,nickname,realname,headimg,sex,tel,money,score,levelid,createtime,last_visittime,province,city,remark')
            ->where($where)->page($pagenum,$pernum)->order('last_visittime desc,id desc')->select()->toArray();
		if(!$datalist) $datalist = [];
		foreach($datalist as $k=>$v){
			$datalist[$k]['createtime'] = date('Y-m-d H:i',$v['createtime']);
            $datalist[$k]['last_visittime'] = $v['last_visittime'] ? date('Y-m-d H:i',$v['last_visittime']) : '暂无';
            if(false){}else{
                $datalist[$k]['can_login'] = 0;
            }
		}
		if($pagenum == 1){
			$count = Db::name('member')->where($where)->count();
		}
		$rdata = [];
		$rdata['status'] = 1;
		$rdata['count'] = $count;
		$rdata['datalist'] = $datalist;
		$rdata['auth_data'] = $this->auth_data;
		return $this->json($rdata);
	}
	//会员详情
	public function detail(){
        if(bid > 0 && !in_array('member_code_buy',json_decode($this->user['hexiao_auth_data'],true))){
            //多商户仅此功能可查看会员详情
            return $this->json(['status'=>0,'msg'=>'非法操作']);
        }
		$mid = input('param.mid/d');
		$member = db('member')->field('id,aid,bid,nickname,realname,headimg,sex,tel,money,score,levelid,createtime,last_visittime,province,city,remark')
            ->where(['aid'=>aid,'id'=>$mid])->find();
		$member['levelname'] = db('member_level')->where(['id'=>$member['levelid']])->value('name');
		$member['createtime'] = date('Y-m-d H:i',$member['createtime']);
		$rdata = [];

		$default_cid = Db::name('member_level_category')->where('aid',aid)->where('isdefault', 1)->value('id');
        $default_cid = $default_cid ? $default_cid : 0;

		$rdata['levelList'] = Db::name('member_level')->field('id,name')->where('aid',aid)->where('cid',$default_cid)->order('sort,id')->select()->toArray();
		
		//查看下级订单
		$ordershow=false;
		$member['ordercount'] = 0 + Db::name('shop_order')->where('aid',aid)->where('mid',$mid)->count();
		$member['showrichinfo'] = false;
		$rdata['ordershow'] = $ordershow;
		$rdata['member'] = $member;
		return $this->json($rdata);
	}
	//充值
	public function recharge(){
		$mid = input('post.rechargemid/d');
		$money = floatval(input('post.rechargemoney'));
		if($money == 0){
			return $this->json(['status'=>0,'msg'=>'请输入充值金额']);
		}
		$info = db('member')->where('aid',aid)->where('id',$mid)->find();
		if(!$info) return $this->json(['status'=>0,'msg'=>'未找到该会员'.t('会员')]);
		\app\common\Member::addmoney(aid,$mid,$money,'商家充值，操作员：'.$this->user['un']);
        return $this->json(['status'=>1,'msg'=>'充值成功']);
	}
	//改积分
	public function addscore(){
		$mid = input('post.rechargemid/d');
		$score = floatval(input('post.rechargescore'));
		if($score == 0){
			return $this->json(['status'=>0,'msg'=>'请输入'.t('积分').'数量']);
		}
		$info = db('member')->where('aid',aid)->where('id',$mid)->find();
		if(!$info) return $this->json(['status'=>0,'msg'=>'未找到该'.t('会员')]);
        $rs = \app\common\Member::addscore(aid,$mid,$score,'商家增加'.t('积分'));
        if($rs['status'] == 0) return $this->json($rs);
		return $this->json(['status'=>1,'msg'=>'操作成功']);
	}
    //改积分
    public function decscore(){
        if(!in_array('member_code_buy',json_decode($this->user['hexiao_auth_data'],true))){
            return $this->json(['status'=>0,'msg'=>'非法操作']);
        }
        if(bid > 0){
            if(!getcustom('business_update_member_score'))
                return $this->json(['status'=>0,'msg'=>'非法操作']);
        }
        $mid = input('post.rechargemid/d');
        $score = floatval(input('post.rechargescore'));
        if($score == 0){
            return $this->json(['status'=>0,'msg'=>'请输入'.t('积分').'数量']);
        }
        $info = db('member')->where('aid',aid)->where('id',$mid)->find();
        if(!$info) return $this->json(['status'=>0,'msg'=>'未找到该'.t('会员')]);
        $rs = \app\common\Member::addscore(aid,$mid,$score,'商家消费，扣除'.t('积分'),'admin',bid);
        if($rs['status'] == 0) return $this->json($rs);
        return $this->json(['status'=>-4,'msg'=>'操作成功','url'=>'/admin/index/index']);
    }
	//备注
	public function remark(){
		$mid = input('post.remarkmid/d');
		$remark = input('post.remark');
		db('member')->where('aid',aid)->where('id',$mid)->update(['remark'=>$remark]);
		return $this->json(['status'=>1,'msg'=>'操作成功']);
	}
	//修改等级
	public function changelv(){
		$mid = input('post.changemid/d');
		$levelid = input('post.levelid');
		db('member')->where('aid',aid)->where('id',$mid)->update(['levelid'=>$levelid]);
		\app\common\Wechat::updatemembercard(aid,$mid);
		return $this->json(['status'=>1,'msg'=>'操作成功']);
	}

    public function history(){
        if(input('param.id/d') < 1){
            return $this->json(['status'=>0,'msg'=>'参数错误']);
        }
        $pagenum = input('post.pagenum');
        if(!$pagenum) $pagenum = 1;
        $pernum = 20;
        $where = [];
        $where[] = ['aid','=',aid];
        $where[] = ['mid','=',input('param.id/d')];
        $datalist = Db::name('member_history')->field('id,proid,from_unixtime(createtime)createtime,type')->where($where)->page($pagenum,$pernum)->order('createtime desc')->select()->toArray();
        if(!$datalist) $datalist = [];
        foreach($datalist as $k=>$v){
            $product = [];
            if($v['type'] == 'shop'){
                $product = Db::name('shop_product')->where('id',$v['proid'])->find();
            }elseif($v['type'] == 'yuyue'){
                $product = Db::name('yuyue_product')->where('id',$v['proid'])->find();
            }elseif($v['type'] == 'collage'){
                $product = Db::name('collage_product')->where('id',$v['proid'])->find();
            }elseif($v['type'] == 'scoreshop'){
                $product = Db::name('scoreshop_product')->where('id',$v['proid'])->find();
            }elseif($v['type'] == 'choujiang'){
                $product = Db::name('choujiang_product')->where('id',$v['proid'])->find();
                $product['market_price'] = $product['sell_price'];
                $product['sell_price'] = $product['min_price'];
            }
            if(!$product){
                Db::name('member_history')->where('id',$v['id'])->delete();
                unset($datalist[$k]);
            }else{
                $datalist[$k]['product'] = $product;
            }
        }
        if(request()->isPost()){
            return $this->json(['status'=>1,'data'=>$datalist]);
        }
        $count = Db::name('member_history')->where($where)->count();

        $rdata = [];
        $rdata['count'] = $count;
        $rdata['datalist'] = $datalist;
        $rdata['pernum'] = $pernum;
        return $this->json($rdata);
    }
    public function searchCode()
    {
        if(!in_array('member_code_buy',json_decode($this->user['hexiao_auth_data'],true))){
            return $this->json(['status'=>0,'msg'=>'非法操作']);
        }
        if(!input('?param.code')){
            return $this->json(['status'=>0,'msg'=>'请输入会员码']);
        }
        $code = input('param.code');
        $member = Db::name('member')->where('aid',aid)->where('member_code',$code)->find();
        if(empty($member)){
            return $this->json(['status'=>0,'msg'=>'会员不存在，请检查会员码']);
        }

        $rdata = [];
        $rdata['status'] = 1;
        $rdata['mid'] = $member['id'];
        return $this->json($rdata);
    }

    //管理员登录会员中心,不太安全 TODO
    public function adminLoginUser(){
	    }

	//详细介绍
	public function richinfo(){
        $mid = input('param.mid/d');
		$member = db('member')->field('id,aid,bid,nickname,realname,headimg,sex,tel,money,score,levelid,createtime,last_visittime,province,city,remark,richinfo')->where(['aid'=>aid,'id'=>$mid])->find();
		if(!$member) return $this->json(['status'=>-4,'msg'=>'未找到该用户']);
		if(request()->isPost()){
			$postinfo = input('post.info/a');
			$richinfo = json_encode(input('post.pagecontent'));
			//$nickname = $postinfo['nickname'];
			Db::name('member')->where('aid',aid)->where('id',$mid)->update(['richinfo'=>$richinfo]);
			return $this->json(['status'=>1,'msg'=>'操作成功']);
		}
		$pagecontent = json_decode(\app\common\System::initpagecontent($member['richinfo'],aid),true);
		if(!$pagecontent) $pagecontent = [];
        $rdata = [];
        $rdata['status'] = 1;
        $rdata['info'] = $member;
        $rdata['pagecontent'] = $pagecontent;
        return $this->json($rdata);
	}
}